#include "information.h"
#include "ui_information.h"
#include "login.h"

Information::Information(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Information)
{
    ui->setupUi(this);
     ui->label_pic->setStyleSheet("QPushButton{border-image: url(://icon/handsomeboy.jpg);border-radius:25px;}");
    socket = new QTcpSocket(this);
    connect(socket, SIGNAL(readyRead()), this, SLOT(readMsg()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(displayError(QAbstractSocket::SocketError)));  //发生错误时执行displayError函数

}

Information::~Information()
{
    delete this->socket;
    delete ui;
}

void Information::readMsg() {//读取传输过来的消息
    QString data = socket->readAll();
    QStringList list = data.split("#");
    if (list[0] == "h" && list[1] == "true") {
        QString str1 = list[2];
        QString str2 = list[3];
        QString str3 = list[4];
        qDebug() << list;
        ui->lineEdit_2->setText(str1);
        ui->lineEdit_3->setText(str2);
        ui->comboBox->setCurrentIndex(str3.toInt());
        int num=str3.toInt();
        qDebug()<<num;
    }else if(list[0]=="j" && list[1]=="true"){
        QString ok="保存成功";
        QMessageBox::information(this, "保存成功", ok, QMessageBox::Ok);
        //Information::close();
    }
}

void Information::on_pushButton_2_clicked()
{
    QString title = windowTitle();
    ui->label_name->setText(title);
    socket->abort();   //取消已有的连接
    socket->connectToHost(IP, port); //连接服务器
    //等待连接成功
    if (!socket->waitForConnected(30000)) {
        qDebug() << "连接失败!";
        return;
    }
    qDebug() << "连接成功!";
    QString string = ui->label_name->text();//用户名
    QString hs = "h";
    QString data = hs + "#" + string;
    socket->write(data.toUtf8());
    //ui->lineEdit->clear();

}

void Information::on_pushButton_clicked()
{
    QString title = windowTitle();
    ui->label_name->setText(title);
    socket->abort();   //取消已有的连接
    socket->connectToHost(IP, port); //连接服务器
    //等待连接成功
    if (!socket->waitForConnected(30000)) {
        qDebug() << "连接失败!";
        return;
    }
    qDebug() << "连接成功!";
    QString str1 = ui->label_name->text();//用户名
    QString str2 = ui->lineEdit_2->text();//用户名
    QString str3 = ui->lineEdit_3->text();//用户名
    int headpic=ui->comboBox->currentIndex();
    QString str4 = QString::number(headpic);
    QString js = "j";
    QString data = js + "#" + str1+"#"+str2+"#"+str3+"#"+str4;
    socket->write(data.toUtf8());
}

void Information::on_comboBox_currentIndexChanged(int num)
{
    qDebug() << num;
    if (num == 0) {
//        map.load("://icon/handsomeboy.jpg");
//        map = map.scaled(QSize(50, 50));
//        ui->label_pic->setPixmap(map
        ui->label_pic->setStyleSheet("QPushButton{border-image: url(://icon/handsomeboy.jpg);border-radius:25px;}");
    }
    if (num == 1) {
//        map.load("://icon/redhair.jpg");
//        map = map.scaled(QSize(50, 50));
//        ui->label_pic->setPixmap(map);
        ui->label_pic->setStyleSheet("QPushButton{border-image: url(://icon/redhair.jpg);border-radius:25px;}");
    }
    if (num == 2) {
//        map.load("://icon/bearded.jpg");
//        map = map.scaled(QSize(50, 50));
//        ui->label_pic->setPixmap(map);
        ui->label_pic->setStyleSheet("QPushButton{border-image: url(://icon/bearded.jpg);border-radius:25px;}");
    }
    if (num == 3) {
//        map.load("://icon/bluedog.jpg");
//        map = map.scaled(QSize(50, 50));
//        ui->label_pic->setPixmap(map);
        ui->label_pic->setStyleSheet("QPushButton{border-image: url(:/icon/bluedog.jpg);border-radius:25px;}");

    }
    if (num == 4) {
//        map.load("://icon/snoopy.jpg");
//        map = map.scaled(QSize(50, 50));
//        ui->label_pic->setPixmap(map);
        ui->label_pic->setStyleSheet("QPushButton{border-image: url(://icon/baldman.jpg);border-radius:25px;}");
    }
    if (num == 5) {
        ui->label_pic->setStyleSheet("QPushButton{border-image: url(://icon/goddess.jpg);border-radius:25px;}");
    }
    if (num == 6) {
//        map.load("://icon/Americansuperstar.jpg");
//        map = map.scaled(QSize(50, 50));
//        ui->label_pic->setPixmap(map);
        ui->label_pic->setStyleSheet("QPushButton{border-image: url(://icon/greenQQ.png);border-radius:25px;}");
    }
    if (num == 7) {
//        map.load("://icon/magicgirl.jpg");
//        map = map.scaled(QSize(50, 50));
//        ui->label_pic->setPixmap(map);
        ui->label_pic->setStyleSheet("QPushButton{border-image: url(://icon/huanqian.jpg);border-radius:25px;}");
    }
    if (num == 8) {
//        map.load("://icon/ourfaith.jpg");
//        map = map.scaled(QSize(50, 50));
//        ui->label_pic->setPixmap(map);
        ui->label_pic->setStyleSheet("QPushButton{border-image: url(://icon/ourfaith.jpg);border-radius:25px;}");
    }
}
